package com.cpfmat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cpfmat.entity.ContentUser;
import com.cpfmat.mapper.ContentUserMapper;
import com.cpfmat.service.IContentUserService;

@Service
public class ContentUserServiceImpl extends ServiceImpl<ContentUserMapper, ContentUser> implements IContentUserService {

	@Autowired
	private ContentUserMapper contentUserMapper;

	@Override
	public int customUpdate(ContentUser contentUser) {
		return contentUserMapper.customUpdate(contentUser);
	}

	@Override
	public Page<ContentUser> getcontentByUser(String userId, int pageNum, int pageSize) {
		Map<String, Object> param = new HashMap<String, Object>();
		if (pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);

		if (StringUtils.isNotBlank(userId)) {
			param.put("userId", userId);
		}

		long counts = contentUserMapper.countList(param);

		List<ContentUser> resultList = null;
		if (counts > 0) {
			resultList = contentUserMapper.getList(param);
		}

		Page<ContentUser> resultPage = new Page<ContentUser>();

		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);
		return resultPage;
	}

	@Override
	public Page<ContentUser> getAllPage(String userName, String subTitle, int pageNum, int pageSize) {
		Map<String, Object> param = new HashMap<String, Object>();
		if (pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);
		
		if (StringUtils.isNotBlank(userName)){
			param.put("userId", userName);
		}
		if (StringUtils.isNotBlank(subTitle)){
			param.put("subTitle", subTitle);
		}

		long counts = contentUserMapper.countAllList(param);

		List<ContentUser> resultList = null;
		if (counts > 0) {
			resultList = contentUserMapper.getAllList(param);
		}

		Page<ContentUser> resultPage = new Page<ContentUser>();

		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);

		return resultPage;
	}

	@Override
	public ContentUser getObjectById(String seqId) {
		if(StringUtils.isNotBlank(seqId)) {
			return contentUserMapper.getObjectById(seqId);
		}
		else {
			return null;
		}
	}

}
